import httplib, urllib, re, socket, time

def getResponse(connection, parameters, headers):
	return tryResponse(connection, parameters, headers, 3)

def tryResponse(connection, parameters, headers, tries):
	try:
		connection.request("POST", "/servlet/schoolsource.command.TeacherAccessCMD", parameters, headers)
		response = connection.getresponse().read()
		connection.close()
		return response
	except httplib.HTTPException, inst:
		print "An HTTPException occurred while trying to communicate with the host"
		print "connection: " + str(connection)
		print "parameters: " + str(parameters)
		print "headers: " + str(headers)
		print inst
		return None
	except socket.error, inst:
		print "Socket error"
		print "connection: " + str(connection)
		print "parameters: " + str(parameters)
		print "headers: " + str(headers)
		print inst
		try:
			connection.close()
		except:
			print "Could not close the connection"
		if tries == 0:
			return None
		else:
			time.sleep(4)
			return tryResponse(connection, parameters, headers, tries - 1)


def testTeacher(conn, headers):
	iter = 0
	#print "Teacher ID: 1000"
	#Teacher logs in
	#//////////////////////////////////////////////////////////////
	params = urllib.urlencode({'USR_SCHOOL_ID': '1000', 'SCHOOLNAME': 'demo', 'submitlogin': 'Login', 'USR_PASSWORD': 'teacher', 'USRTYPE': 'TEACHER'})
	response = getResponse(conn, params, headers)
	iter = iter + 1
	tokenPattern = re.compile("<INPUT TYPE=\"HIDDEN\" NAME=\"TOKEN\" VALUE=\"(\d*)\">")
	match = tokenPattern.search(response)
	if None == match or None == match.group(1):
		print "Could not find token. Aborting..."
		return iter
	
	token = match.group(1)
	#print token
	for j in range(0, 5):
		#print "j:",j
		#View classes
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'CURRENT': 'TEACHERCLASSINFO', 'PARAM': j, 'LINKCLICKED': 'TEACHERCLASSINFO', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'CLASSNUM': j, 'USRTYPE': 'TEACHER'})
		response = getResponse(conn, params, headers)
		iter = iter + 1
		mailPattern = re.compile('setSelected\(0\)\;goToNextPage\(\'MAILLINK\'')
		mailmatch = mailPattern.search(response)

		while mailmatch:
			#Read first email
			params = urllib.urlencode({'TOKEN':  token, 'SCHOOLNAME':  'demo', 'USRTYPE':  'TEACHER', 'CURRENT':  'SUBFRAME', 'PARAM':  '0', 'LINKCLICKED':  'MAILLINK', 'CLASSNUM':  j})
			response = getResponse(conn, params, headers)
			iter = iter + 1

			#Delete first email
			params = urllib.urlencode({
			'MAILNUM': '0', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'FORMFROMPARENT': 'FALSE', 'USRTYPE': 'TEACHER', 'CURRENT': 'SUBFRAME', 'PARAM': '0', 'ASSIGNEDRECIPIENTS': '', 'LINKCLICKED': 'DELMAIL', 'FORMFROMSTUDENT': 'TRUE', 'CT_FROM_ID': '237', 'CLASSNUM': j, 'CT_TO_ID': '84'})
			response = getResponse(conn, params, headers)
			iter = iter + 1

			params = urllib.urlencode({'CURRENT': 'TEACHERCLASSINFO', 'PARAM': j, 'LINKCLICKED': 'TEACHERCLASSINFO', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'CLASSNUM': j, 'USRTYPE': 'TEACHER'})
			response = getResponse(conn, params, headers)
			iter = iter + 1
			mailPattern = re.compile('setSelected\(0\)\;goToNextPage\(\'MAILLINK\'')
			mailmatch = mailPattern.search(response)

		for i in range(0, 21):
			#print "i:",i
			#New email
			#//////////////////////////////////////////////////////////////
			params = urllib.urlencode({'CURRENT': 'SUBFRAME', 'PARAM': '-1', 'LINKCLICKED': 'MAILLINK', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'CLASSNUM': j, 'USRTYPE': 'TEACHER'})
			response = getResponse(conn, params, headers)
			iter = iter + 1
			classPattern = re.compile("<INPUT TYPE=\"HIDDEN\" NAME=\"CLASS_ASSIGNED_ID\" VALUE=\"(\d*)\">")
			classMatch = classPattern.search(response)
			if None == classMatch or None == classMatch.group(1):
				print "Could not find class id. Aborting"
				print str(params)
				return iter
			class_id = classMatch.group(1)

			to_ids = str(2 + (2 * i)) + "," + str(43 + (2 * i))

			#Send email
			#//////////////////////////////////////////////////////////////
			params = urllib.urlencode({'CLASS_ASSIGNED_ID': class_id, 'MSG_SUBJECT': 'Test 1', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'FORMFROMPARENT': 'FALSE', 'BUTTONCLICKED': 'Send', 'MSG_BODY': 'This is a test email.\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n', 'USRTYPE': 'TEACHER', 'CURRENT': 'SUBFRAME', 'PARAM': '', 'ASSIGNEDRECIPIENTS': to_ids, 'LINKCLICKED': '', 'FORMFROMSTUDENT': 'TRUE', 'CT_FROM_ID': '84', 'CLASSNUM': j})
			response = getResponse(conn, params, headers)
			iter = iter + 1

		#Refresh
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'TOKEN': token, 'SCHOOLNAME': 'demo', 'USRTYPE': 'TEACHER', 'CURRENT': 'TEACHERCLASSINFO', 'PARAM': '-1', 'LINKCLICKED': 'Refresh', 'CLASSNUM': j})
		response = getResponse(conn, params, headers)
		iter = iter + 1

		#New mail
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'CURRENT': 'SUBFRAME', 'PARAM': '-1', 'LINKCLICKED': 'MAILLINK', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'CLASSNUM': j, 'USRTYPE': 'TEACHER'})
		response = getResponse(conn, params, headers)
		iter = iter + 1

		#Send email
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'CLASS_ASSIGNED_ID': class_id, 'MSG_SUBJECT': 'Test 2', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'FORMFROMPARENT': 'FALSE', 'BUTTONCLICKED': 'Send', 'MSG_BODY': 'This is a test email to all students in the class.\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n', 'USRTYPE': 'TEACHER', 'CURRENT': 'SUBFRAME', 'PARAM': '', 'ASSIGNEDRECIPIENTS': '2,4,6,8,10,12,14,16,18,20,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83', 'LINKCLICKED': '', 'FORMFROMSTUDENT': 'TRUE', 'CT_FROM_ID': '84', 'CLASSNUM': j})
		response = getResponse(conn, params, headers)
		iter = iter + 1

		#Refresh
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'TOKEN': token, 'SCHOOLNAME': 'demo', 'USRTYPE': 'TEACHER', 'CURRENT': 'TEACHERCLASSINFO', 'PARAM': '-1', 'LINKCLICKED': 'Refresh', 'CLASSNUM': j})
		response = getResponse(conn, params, headers)
		iter = iter + 1

		#New homework
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'CURRENT': 'SUBFRAME', 'PARAM': '-1', 'LINKCLICKED': 'HWLINK', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'CLASSNUM': j, 'USRTYPE': 'TEACHER'})
		response = getResponse(conn, params, headers)
		iter = iter + 1

		#Save homework
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'ATT_FILE_NAME': '/home/mystud2/public_html/WEB-INF/tmp/form579400763.tmp', 'DELATTACHMENT': '', 'CLASS_ASSIGNED_ID': class_id, 'HW_DATE_DUE': '08/31/2004', 'HW_TOTAL_POINTS': '100', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'HW_DESCRIPTION': 'This is a test homework.\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n', 'USRTYPE': 'TEACHER', 'ATT_FILE_NAME.filename' : '', 'CURRENT': 'SUBFRAME', 'PARAM': '', 'HW_ANSWERS': 'This is an answer.\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890', 'LINKCLICKED': 'Assign', 'HW_DATE_ASSIGNED': '08/28/2004', 'HW_SHORT_NAME': 'Test 1', 'CLASSNUM': j, 'HW_NOTES': 'These are some notes.\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n', 'HW_ASSIGNED_ID': '-1', 'ATT_FILE_NAME.file': '/home/mystud2/public_html/WEB-INF/tmp/form579400763.tmp', 'ATT_FILE_NAME.content-type': 'application/octet-stream'})
		response = getResponse(conn, params, headers)
		iter = iter + 1

		#Refresh
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'TOKEN': token, 'SCHOOLNAME': 'demo', 'USRTYPE': 'TEACHER', 'CURRENT': 'TEACHERCLASSINFO', 'PARAM': '-1', 'LINKCLICKED': 'Refresh', 'CLASSNUM': j})
		response = getResponse(conn, params, headers)
		iter = iter + 1

		#View homework
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'TOKEN': token, 'SCHOOLNAME': 'demo', 'USRTYPE': 'TEACHER', 'CURRENT': 'SUBFRAME', 'PARAM': '0', 'LINKCLICKED': 'HWLINK', 'CLASSNUM': j})
		response = getResponse(conn, params, headers)
		iter = iter + 1
		hwPattern = re.compile("<INPUT TYPE=\"HIDDEN\" NAME=\"HW_ASSIGNED_ID\" VALUE=\"(\d*)\">")
		hwMatch = hwPattern .search(response)
		if None == hwMatch or None == hwMatch.group(1):
			print "Could not find homework assigned id."
		else:
			hw_id = hwMatch.group(1)
			#print class_id

			#Grade homework
			#//////////////////////////////////////////////////////////////
			params = urllib.urlencode({'ATT_FILE_NAME': '/home/mystud2/public_html/WEB-INF/tmp/form2090694443.tmp', 'DELATTACHMENT': '', 'CLASS_ASSIGNED_ID': class_id, 'HW_DATE_DUE': '08/31/2004', 'HW_TOTAL_POINTS': '100', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'HW_DESCRIPTION': 'This is a test homework.\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n', 'USRTYPE': 'TEACHER', 'ATT_FILE_NAME.filename': '', 'CURRENT': 'SUBFRAME', 'PARAM': '0', 'HW_ANSWERS': 'This is an answer.\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n', 'LINKCLICKED': 'GRADELINK', 'HW_DATE_ASSIGNED': '08/28/2004', 'HW_SHORT_NAME': 'Test 1', 'CLASSNUM': j, 'HW_NOTES': 'These are some notes.\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n', 'HW_ASSIGNED_ID': hw_id, 'ATT_FILE_NAME.file': '/home/mystud2/public_html/WEB-INF/tmp/form2090694443.tmp', 'ATT_FILE_NAME.content-type': 'application/octet-stream'})
			response = getResponse(conn, params, headers)
			iter = iter + 1

		#Save grades
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'GRADE_SCORE2': '100.0', 'GRADE_COMMENT2': 'Great job!', 'GRADE_COMMENT4': 'Great job!', 'GRADE_COMMENT6': 'Great job!', 'BUTTONCLICKED': 'Save Grade', 'GRADE_SCORE4': '100.0', 'USRTYPE': 'TEACHER', 'CURRENT': 'SUBFRAME', 'GRADE_COMMENT8': 'Great job!', 'LINKCLICKED': '', 'GRADE_SCORE6': '100.0', 'GRADE_COMMENT10': 'Great job!', 'GRADE_SCORE8': '100.0', 'GRADE_SCORE10': '100.0', 'GRADE_COMMENT12': 'Great job!', 'GRADE_SCORE12': '100.0', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'PARAM': '', 'GRADE_COMMENT14': 'Great job!', 'GRADE_COMMENT16': 'Great job!', 'GRADE_SCORE12': '100.0', 'GRADE_SCORE14': '100.0', 'GRADE_SCORE16': '100.0', 'GRADE_COMMENT18': 'Great job!', 'GRADE_SCORE18': '100.0', 'GRADE_SCORE20': '100.0', 'GRADE_COMMENT20': 'Great job!', 'GRADE_SCORE22': '100.0', 'GRADE_COMMENT22': 'Great job!', 'GRADE_SCORE24': '100.0', 'GRADE_COMMENT24': 'Great job!', 'GRADE_COMMENT26': 'Great job!', 'GRADE_SCORE26': '100.0', 'GRADE_SCORE28': '100.0', 'GRADE_SCORE30': '100.0', 'GRADE_SCORE32': '100.0', 'GRADE_SCORE34': '100.0', 'GRADE_COMMENT28': 'Great job!', 'CLASSNUM': j, 'GRADE_COMMENT30': 'Great job!', 'GRADE_COMMENT32': 'Great job!', 'HW_ASSIGNED_ID': '28', 'GRADE_COMMENT34': 'Great job!', 'GRADE_COMMENT36': 'Great job!', 'GRADE_COMMENT38': 'Great job!', 'GRADE_SCORE36': '100.0', 'GRADE_COMMENT40': 'Great job!', 'GRADE_SCORE38': '100.0', 'GRADE_COMMENT42': 'Great job!', 'GRADE_SCORE40': '100.0', 'GRADE_SCORE42': '100.0'})
		response = getResponse(conn, params, headers)
		iter = iter + 1

		#Refresh (not needed)
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'USRTYPE': 'TEACHER', 'CURRENT': 'TEACHERCLASSINFO', 'LINKCLICKED': 'Refresh', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'PARAM': '-1', 'CLASSNUM': j})
		response = getResponse(conn, params, headers)
		iter = iter + 1

		#Enrollment
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'CURRENT': 'SUBFRAME', 'PARAM': '-1', 'LINKCLICKED': 'ENROLLMENTLINK', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'CLASSNUM': j, 'USRTYPE': 'TEACHER'})
		response = getResponse(conn, params, headers)
		iter = iter + 1

		#Student profile
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'TOKEN': token, 'SCHOOLNAME': 'demo', 'selection': 'STUDENTPROFILE', 'USRTYPE': 'TEACHER', 'SELECTEDSTUDENT': '245', 'CURRENT': 'SUBFRAME', 'PARAM': '245', 'LINKCLICKED': 'STUDENTPROFILE', 'CLASSNUM': j})
		response = getResponse(conn, params, headers)
		iter = iter + 1

		#Enrollment
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'USRTYPE': 'TEACHER', 'CURRENT': 'SUBFRAME', 'USR_TYPE': 'TEACHER', 'LINKCLICKED': 'ENROLLMENTLINK', 'CLASSNUM': j, 'TOKEN': token, 'SCHOOLNAME': 'demo', 'PARAM': '-1'})
		response = getResponse(conn, params, headers)
		iter = iter + 1

		#Student progress report
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'USRTYPE': 'TEACHER', 'SELECTEDSTUDENT': '245', 'CURRENT': 'SUBFRAME', 'LINKCLICKED': 'PROGRESSLINK', 'CLASSNUM': j, 'TOKEN': token, 'SCHOOLNAME': 'demo', 'selection': 'PROGRESSLINK', 'PARAM': '245'})
		response = getResponse(conn, params, headers)
		iter = iter + 1

		#View Homework
		#//////////////////////////////////////////////////////////////
		params = urllib.urlencode({'CURRENT': 'SUBFRAME', 'PARAM': '0', 'LINKCLICKED': 'HWLINK', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'CLASSNUM': j, 'USRTYPE': 'TEACHER'})
		response = getResponse(conn, params, headers)
		iter = iter + 1

		#Delete Homework
		#//////////////////////////////////////////////////////////////
		#params = urllib.urlencode({'ATT_FILE_NAME': '/home/mystud2/public_html/WEB-INF/tmp/form621201340.tmp', 'DELATTACHMENT': '', 'CLASS_ASSIGNED_ID': class_id, 'HW_DATE_DUE': '08/31/2004', 'HW_TOTAL_POINTS': '100', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'HW_DESCRIPTION': 'This is a test homework.\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n', 'USRTYPE': 'TEACHER', 'ATT_FILE_NAME.filename': '', 'CURRENT': 'SUBFRAME', 'PARAM': '0', 'HW_ANSWERS': 'This is an answer.\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n', 'LINKCLICKED': 'DELHOMEWORK', 'HW_DATE_ASSIGNED': '08/28/2004', 'HW_SHORT_NAME': 'Test 1', 'CLASSNUM': j, 'HW_NOTES': 'These are some notes.\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n123456789012345678901234567890123456789012345678901234567890\r\n', 'HW_ASSIGNED_ID': '28', 'ATT_FILE_NAME.file': '/home/mystud2/public_html/WEB-INF/tmp/form621201340.tmp', 'ATT_FILE_NAME.content-type': 'application/octet-stream'})
		#response = getResponse(conn, params, headers)
		iter = iter + 1

		#Refresh
		#//////////////////////////////////////////////////////////////
		#params = urllib.urlencode({'TOKEN': token, 'SCHOOLNAME': 'demo', 'USRTYPE': 'TEACHER', 'CURRENT': 'TEACHERCLASSINFO', 'PARAM': '-1', 'LINKCLICKED': 'Refresh', 'CLASSNUM': j})
		#response = getResponse(conn, params, headers)
		iter = iter + 1

	#Logout
	#//////////////////////////////////////////////////////////////
	params = urllib.urlencode({'USRTYPE': 'TEACHER', 'CURRENT': 'TEACHERCLASSINFO', 'LINKCLICKED': 'LOGOUT', 'TOKEN': token, 'SCHOOLNAME': 'demo', 'PARAM': '-1', 'CLASSNUM': '0'})
	response = getResponse(conn, params, headers)
	iter = iter + 1
	return iter